package com.yuntsg.ruiijn.paperana.utils;

import lombok.SneakyThrows;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @Author: 葛成瑞
 * @Date: 2021/7/18 20:27
 */

public class SmallTool {

    @SneakyThrows
    public static void SleepMillis(Long l) {
        printMess("睡眠开始+++" + l);
        Thread.sleep(l);

    }

    @SneakyThrows
    public static void SleepMillis(int l) {
        printMess("睡眠开始+++" + l);
        Thread.sleep(l);

    }

    public static void sout(String string, String color) {
        Map<String, String> map = new HashMap<>();
        map.put("黑色", "30");
        map.put("红色", "31");
        map.put("绿色", "32");
        map.put("黄色", "33");
        map.put("蓝色", "34");
        map.put("紫红色", "35");
        map.put("青蓝色", "36");
        map.put("白色", "37");
        Scanner scanner = new Scanner(System.in);
//        System.out.println("请输入想要的颜色：黑色，红色，绿色，黄色，蓝色，紫红色，青蓝色，白色");
        String colorCode = null;
        colorCode = map.get(color);
        System.out.println("\033[1;" + colorCode + "m" + string + "\033[0m");
    }

    public static void printMemory() {
        String freeMemory = Runtime.getRuntime().freeMemory() / 1024 / 1024 + " MB";
        String totalMemory = Runtime.getRuntime().totalMemory() / 1024 / 1024 + " MB";
        String maxMemory = Runtime.getRuntime().maxMemory() / 1024 / 1024 + " MB";
        printMess("当前能够申请到的最大内存--" + maxMemory + "--当前空余 " + freeMemory + "--已经从操作系统获取的内存--" + totalMemory);
    }


    public static void printMess(Object s) {
        if (s != null) {
            String res = new StringJoiner("\t|\t")
                    .add(longToDate1(System.currentTimeMillis()))
                    .add(String.valueOf(Thread.currentThread().getId()))
                    .add(Thread.currentThread().getName())
                    .add(s.toString())
                    .toString();
            // 默认显示 青蓝色
            sout(res, "绿色");
//        MyUtils.sout(res, "青蓝色");
//        System.out.println(res);
        }


    }

    public static void printMessDown(Object s) {
        if (s != null) {
            String res = new StringJoiner("\t|\t")
                    .add(longToDate1(System.currentTimeMillis()))
                    .add(String.valueOf(Thread.currentThread().getId()))
                    .add(Thread.currentThread().getName())
                    .add(s.toString())
                    .toString();
            // 默认显示 青蓝色
            sout(res, "青蓝色");
//        MyUtils.sout(res, "青蓝色");
//        System.out.println(res);
        }


    }

    public static void printMessError(Object s) {
        if (s != null) {
            String res = new StringJoiner("\t|\t")
                    .add(longToDate1(System.currentTimeMillis()))
                    .add(String.valueOf(Thread.currentThread().getId()))
                    .add(Thread.currentThread().getName())
                    .add(s.toString())
                    .toString();
            sout(res, "黄色");
//        System.err.println(res);
        }


    }


    // 输出与上个耗时

    /**
     * @param s
     * @param start
     */
    public synchronized static void printMessLongTimeTake(Object s, Long start) {
        long l = System.currentTimeMillis();
        String res = new StringJoiner("\t|\t")
                .add(longToDate1(l))
                .add(String.valueOf(Thread.currentThread().getId()))
                .add(Thread.currentThread().getName())
                .add(s.toString() + "---耗时---" + (l - start) / 1000 + "秒")
                .toString();
        sout(res, "紫红色");
//        System.out.println(res);
    }

    public static String longToDate1(long i) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = new Date(i);
        String str = sdf.format(date);
        return str;
    }
}
